{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f12761a8",
   "metadata": {},
   "source": [
    "## 2022-2023-02\n",
    "\n",
    "## Python Data Analysis Course(PDAC)  \n",
    "  \n",
    "## 笔记：江柯廷 \n",
    "  \n",
    "## week02：数据理解"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3747929b",
   "metadata": {},
   "source": [
    "# 本周重点知识：  \n",
    "  \n",
    "## Python基础  \n",
    "* 1.匿名函数与 map 方法  \n",
    "* 2.zip 对象与 enumerate 方法  \n",
    "  \n",
    "## 文件的读取和写入(P27)  \n",
    "* 1.csv  \n",
    "* 2.excel  \n",
    "* 3.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2a99b4f",
   "metadata": {},
   "source": [
    "## 基本的数据结构（P31）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a5bc37c",
   "metadata": {},
   "source": [
    "# Python基础  \n",
    "  \n",
    "## 匿名函数与map方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c2e58bb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 匿名函数\n",
    "[(lambda x:2*x)(i) for i in range(5)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "62ab5035",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x: 2*x,range(5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d89d7bfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 学生练习：\n",
    "name = ['小明','小红','李华']\n",
    "age = [18,19,17]\n",
    "\n",
    "# # 希望得到效果 ： \n",
    "# ['小明_18','小红_19','李华_17'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6a46c85c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明_18\n",
      "小明_19\n",
      "小明_17\n",
      "小红_18\n",
      "小红_19\n",
      "小红_17\n",
      "李华_18\n",
      "李华_19\n",
      "李华_17\n"
     ]
    }
   ],
   "source": [
    "# 错误示范\n",
    "for i in name:\n",
    "    for j in age:\n",
    "        print(i+'_'+str(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "92011ca8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明_18', '小红_19', '李华_17']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正确方法\n",
    "list(map(lambda x,y:x+'_'+str(y),name,age))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3d2c13f",
   "metadata": {},
   "source": [
    "# zip对象与enumera方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9aa2163a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# zip 方法\n",
    "L1,L2,L3 = list('abc'),list('def'),list('hij')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e471b8ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(L1,L2,L3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7c876327",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pyecharts\n",
      "  Downloading pyecharts-2.0.2-py3-none-any.whl (146 kB)\n",
      "Collecting prettytable\n",
      "  Downloading prettytable-3.6.0-py3-none-any.whl (27 kB)\n",
      "Requirement already satisfied: jinja2 in c:\\users\\asus\\anaconda3\\lib\\site-packages (from pyecharts) (2.11.3)\n",
      "Collecting simplejson\n",
      "  Downloading simplejson-3.18.3-cp39-cp39-win_amd64.whl (74 kB)\n",
      "Requirement already satisfied: MarkupSafe>=0.23 in c:\\users\\asus\\anaconda3\\lib\\site-packages (from jinja2->pyecharts) (2.0.1)\n",
      "Requirement already satisfied: wcwidth in c:\\users\\asus\\anaconda3\\lib\\site-packages (from prettytable->pyecharts) (0.2.5)\n",
      "Installing collected packages: simplejson, prettytable, pyecharts\n",
      "Successfully installed prettytable-3.6.0 pyecharts-2.0.2 simplejson-3.18.3\n"
     ]
    }
   ],
   "source": [
    "!pip install pyecharts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "99fbcf61",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import BMap\n",
    "from pyecharts.faker import Faker\n",
    "\n",
    "c = (\n",
    "    BMap()\n",
    "    .add_schema(baidu_ak=\"FAKE_AK\", center=[120.13066322374, 30.240018034923])\n",
    "    .add(\n",
    "        \"bmap\",\n",
    "        [list(z) for z in zip(Faker.provinces, Faker.values())],\n",
    "        label_opts=opts.LabelOpts(formatter=\"{b}\"),\n",
    "    )\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"BMap-基本示例\"))\n",
    "    .render(\"bmap_base.html\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e6728dd9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['广东省', '北京市', '上海市', '江西省', '湖南省', '浙江省', '江苏省']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Faker.provinces"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7f02971c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[130, 95, 82, 113, 27, 142, 101]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Faker.values()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a99a2ff",
   "metadata": {},
   "source": [
    "# 文件的读取和写入（P27）\n",
    "##  数据读取  \n",
    "* 数据源  \n",
    "* 1.data/my_csv.csv  \n",
    "* 2.data/my_table.txt  \n",
    "* 3.data/my_excel.xlsx\n",
    "* 4.data/my_table.txt\n",
    "* 5.data/my_csv.csv\n",
    "* 6.data/my_table.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6ca9a29a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0caa455",
   "metadata": {},
   "source": [
    "# 数据写入  \n",
    "* 数据存入  \n",
    "* 1.data/my_csv_saved.csv\n",
    "* 2.data/my_excel_saved.xlsx\n",
    "* 3.data/my_txt_saved.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "83132555",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>排名</td>\n",
       "      <td>排名变化</td>\n",
       "      <td>企业</td>\n",
       "      <td>价值（亿元人民币）</td>\n",
       "      <td>价值变化（亿元人民币）</td>\n",
       "      <td>总部</td>\n",
       "      <td>行业</td>\n",
       "      <td>成立年份</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>抖音</td>\n",
       "      <td>13400</td>\n",
       "      <td>-10050</td>\n",
       "      <td>北京</td>\n",
       "      <td>社交媒体</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>SpaceX</td>\n",
       "      <td>8400</td>\n",
       "      <td>1680</td>\n",
       "      <td>洛杉矶</td>\n",
       "      <td>航天</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>-1</td>\n",
       "      <td>蚂蚁集团</td>\n",
       "      <td>8000</td>\n",
       "      <td>-2010</td>\n",
       "      <td>杭州</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>Stripe</td>\n",
       "      <td>4100</td>\n",
       "      <td>-2230</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>11</td>\n",
       "      <td>Shein</td>\n",
       "      <td>4000</td>\n",
       "      <td>2680</td>\n",
       "      <td>广州</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>15</td>\n",
       "      <td>币安</td>\n",
       "      <td>3000</td>\n",
       "      <td>2010</td>\n",
       "      <td>马耳他</td>\n",
       "      <td>区块链</td>\n",
       "      <td>2017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>Databricks</td>\n",
       "      <td>2500</td>\n",
       "      <td>0</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>大数据</td>\n",
       "      <td>2013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>微众银行</td>\n",
       "      <td>2200</td>\n",
       "      <td>200</td>\n",
       "      <td>深圳</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>京东科技</td>\n",
       "      <td>2000</td>\n",
       "      <td>0</td>\n",
       "      <td>北京</td>\n",
       "      <td>数字科技</td>\n",
       "      <td>2013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>Checkout.com</td>\n",
       "      <td>1900</td>\n",
       "      <td>870</td>\n",
       "      <td>伦敦</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     0     1             2          3            4    5     6     7\n",
       "0   排名  排名变化            企业  价值（亿元人民币）  价值变化（亿元人民币）   总部    行业  成立年份\n",
       "1    1     0            抖音      13400       -10050   北京  社交媒体  2012\n",
       "2    2     1        SpaceX       8400         1680  洛杉矶    航天  2002\n",
       "3    3    -1          蚂蚁集团       8000        -2010   杭州  金融科技  2014\n",
       "4    4     0        Stripe       4100        -2230  旧金山  金融科技  2010\n",
       "5    5    11         Shein       4000         2680   广州  电子商务  2012\n",
       "6    6    15            币安       3000         2010  马耳他   区块链  2017\n",
       "7    7     1    Databricks       2500            0  旧金山   大数据  2013\n",
       "8    8     3          微众银行       2200          200   深圳  金融科技  2014\n",
       "9    9     2          京东科技       2000            0   北京  数字科技  2013\n",
       "10  10    11  Checkout.com       1900          870   伦敦  金融科技  2012"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_hurun = pd.read_html('https://hurun.net/zh-CN/Info/Detail?num=L9SQPH9FKJB1')[0]\n",
    "df_hurun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "59efb757",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_hurun.to_excel('output_hurun.xlsx',index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fdfba838",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_hurun.to_csv('output_hurun.csv',index = False,encoding='UTF8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8eba726f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_hurun.to_csv('output_hurun.txt',sep='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "127fcdc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: tabulate in c:\\users\\asus\\anaconda3\\lib\\site-packages (0.8.9)\n"
     ]
    }
   ],
   "source": [
    "!pip install tabulate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "28e4bf47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|    | 0    | 1        | 2            | 3                  | 4                      | 5      | 6        | 7        |\n",
      "|---:|:-----|:---------|:-------------|:-------------------|:-----------------------|:-------|:---------|:---------|\n",
      "|  0 | 排名 | 排名变化 | 企业         | 价值（亿元人民币） | 价值变化（亿元人民币） | 总部   | 行业     | 成立年份 |\n",
      "|  1 | 1    | 0        | 抖音         | 13400              | -10050                 | 北京   | 社交媒体 | 2012     |\n",
      "|  2 | 2    | 1        | SpaceX       | 8400               | 1680                   | 洛杉矶 | 航天     | 2002     |\n",
      "|  3 | 3    | -1       | 蚂蚁集团     | 8000               | -2010                  | 杭州   | 金融科技 | 2014     |\n",
      "|  4 | 4    | 0        | Stripe       | 4100               | -2230                  | 旧金山 | 金融科技 | 2010     |\n",
      "|  5 | 5    | 11       | Shein        | 4000               | 2680                   | 广州   | 电子商务 | 2012     |\n",
      "|  6 | 6    | 15       | 币安         | 3000               | 2010                   | 马耳他 | 区块链   | 2017     |\n",
      "|  7 | 7    | 1        | Databricks   | 2500               | 0                      | 旧金山 | 大数据   | 2013     |\n",
      "|  8 | 8    | 3        | 微众银行     | 2200               | 200                    | 深圳   | 金融科技 | 2014     |\n",
      "|  9 | 9    | 2        | 京东科技     | 2000               | 0                      | 北京   | 数字科技 | 2013     |\n",
      "| 10 | 10   | 11       | Checkout.com | 1900               | 870                    | 伦敦   | 金融科技 | 2012     |\n"
     ]
    }
   ],
   "source": [
    "print(df_hurun.to_markdown())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a49a532",
   "metadata": {},
   "source": [
    "# 基本的数据结构（P31)   \n",
    "## Series"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6322cedc",
   "metadata": {},
   "source": [
    "## DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7a78d78",
   "metadata": {},
   "source": [
    "# 常用基本函数  \n",
    "* 1.汇总函数  、\n",
    "* 2.特征统计函数  \n",
    "* 3.唯一值函数  \n",
    "* 4.替换函数  \n",
    "* 5.排序函数"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
